package com.example.eda.consumer;

import com.example.eda.event.LoginEvent;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.time.format.DateTimeFormatter;

@Component
public class LoginGreetingConsumer {

    @RabbitListener(queues = "login_queue")
    public void handleLoginEvent(@Payload LoginEvent event) {
        String greeting = String.format(
                "亲爱的 %s，欢迎回来！您的登录时间为：%s",
                event.getUsername(),
                event.getLoginTime().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
        );

        // 实际发送逻辑（示例为打印，可替换为邮件/短信服务）
        System.out.println("发送问候消息：" + greeting);
        sendGreetingNotification(event.getUserId(), greeting);
    }

    private void sendGreetingNotification(Long userId, String message) {
        // 实现实际的发送逻辑（邮件/短信/站内信）
    }
}

